﻿using System.Collections.Generic;

namespace Wisdom.Core.Repositories
{
    /// <summary>
    /// 实体分页类
    /// </summary>
    /// <typeparam name="T">对象类型.</typeparam>
    public class PagedResult<T> : ICollection<T>
    {
        #region Ctor
        /// <summary>
        /// 构造函数：初始化<c>PagedResult</c> 类.
        /// </summary>
        public PagedResult()
        {
            this.data = new List<T>();
        }
        /// <summary>
        /// 构造函数：初始化 <c>PagedResult</c> 类.
        /// </summary>
        /// <param name="totalRecords">集合中总记录数.</param>
        /// <param name="totalPages">总页数.</param>
        /// <param name="pageSize">页大小.</param>
        /// <param name="pageNumber">当前页索引号.</param>
        /// <param name="data">当前页的所有对象.</param>
        public PagedResult(int? totalRecords, int? totalPages, int? pageSize, int? pageNumber, IList<T> data)
        {
            this.totalRecords = totalRecords;
            this.totalPages = totalPages;
            this.pageSize = pageSize;
            this.pageNumber = pageNumber;
            this.data = data;
        }
        #endregion

        #region Public Properties
        private int? totalRecords;
        /// <summary>
        /// 获取或设置总记录数
        /// </summary>
        public int? TotalRecords
        {
            get { return totalRecords; }
            set { totalRecords = value; }
        }

        private int? totalPages;
        /// <summary>
        /// 获取或设置总页数
        /// </summary>
        public int? TotalPages
        {
            get { return totalPages; }
            set { totalPages = value; }
        }

        private int? pageSize;
        /// <summary>
        /// 获取或设置每页的记录数
        /// </summary>
        public int? PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        private int? pageNumber;
        /// <summary>
        /// 获取或设置页的索引
        /// </summary>
        public int? PageNumber
        {
            get { return pageNumber; }
            set { pageNumber = value; }
        }

        private readonly IList<T> data;
        /// <summary>
        /// 获取或设置指定类型<c>PagedResult{T}</c>包含的全部对象实例
        /// </summary>
        public IEnumerable<T> Data
        {
            get { return data; }
        }
        #endregion

        #region ICollection<T> Members
        /// <summary>
        /// 添加一个项到System.Collections.Generic.ICollection{T}
        /// </summary>
        /// <param name="item">要添加的对象.</param>
        public void Add(T item)
        {
            data.Add(item);
        }
        /// <summary>
        /// 从集合中移出所有项
        /// </summary>
        public void Clear()
        {
            data.Clear();
        }
        /// <summary>
        /// 是否包含指定的项
        /// </summary>
        /// <param name="item">要判断的项</param>
        /// <returns>true 包含; 否则, false.</returns>
        public bool Contains(T item)
        {
            return data.Contains(item);
        }
        /// <summary>
        /// 复制System.Collections.Generic.ICollection{T}中的项到指定的数组
        /// </summary>
        /// <param name="array">目标数组</param>
        /// <param name="arrayIndex">从0开始的索引.</param>
        public void CopyTo(T[] array, int arrayIndex)
        {
            data.CopyTo(array, arrayIndex);
        }
        /// <summary>
        /// 获取集合中的元素
        /// </summary>
        public int Count
        {
            get { return data.Count; }
        }
        /// <summary>
        /// 判断集合中的项是否为只读
        /// </summary>
        public bool IsReadOnly
        {
            get { return false; }
        }
        /// <summary>
        /// 从集合中移出符合条件的第一项
        /// </summary>
        /// <param name="item">要判断的项.</param>
        /// <returns></returns>
        public bool Remove(T item)
        {
            return data.Remove(item);
        }

        #endregion

        #region IEnumerable<T> Members
        /// <summary>
        /// 通过一个集合获得一个枚举器
        /// </summary>
        /// <returns>可迭代的枚举器.</returns>
        public IEnumerator<T> GetEnumerator()
        {
            return data.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members
        /// <summary>
        ///通过一个集合获得一个枚举器
        /// </summary>
        /// <returns>可迭代的枚举器.</returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return data.GetEnumerator();
        }

        #endregion
    }
}